//
// Created by wkm on 2024-04-01.
//

#include "x2time.h"

void x2time::prc_x2time() {

    if (reset) {
        done = waiting_task;
        next_done = waiting_task;
        next_result = 0;
        next_times = 0;
        return;
    }

    if (start && done == waiting_task) {
        //收到start为true的那一个时钟信号，就应该从输入端口获取数据，而且此函数只需要执行1次即可
        times = compute_times;
        result = input_data;
        next_done = preparing_task;
    }

    if (done == preparing_task) {
        // 准备数据阶段，把内部状态中需要准备的所有数据都初始化好
        next_times = compute_times;
        next_result = input_data;
        next_done = computing_task;
    }

    if (done == computing_task) {
//            cout << "compute_times:" << compute_times << endl;
        if (times >= 1) {
            if (result.read() & 0x80) {
                next_result = ((result.read() << 1) ^ 0x1B) & 0xFF;
            } else {
                next_result = result.read() << 1;
            }
            next_times = times - 1;
        } else {
            next_done = task_finished;
//                cout << "x2_res:" << next_result << endl;
        }
    }

    times = next_times;
    result = next_result;
    done = next_done;
}